var app = getApp(),
  $ = app.requirejs('jquery'),
  core = app.requirejs('core');
var api = require("../api.js")

module.exports = {
  get: function ($this, callback) {
    var self = this;
    // 获取当前页面
    let currentPage = '/' + getCurrentPages()[getCurrentPages().length - 1].route
    const options = getCurrentPages()[getCurrentPages().length - 1].options
    for (let key in options) {
      const value = options[key]
      if (key == 'id') {
        currentPage = currentPage + '?'
        currentPage += `${key}=${value}`
      }
      if (key == 'pageid') {
        currentPage = currentPage + '?'
        currentPage += `${key}=${value}`
      }
    }

    if ($this.data.currentTab) {
      if (currentPage.includes('/pages/goods/index/index')) {
        if ($this.data.currentTab == '1') {
          currentPage = currentPage
        } else {
          currentPage = currentPage + '?cate=' + $this.data.currentTab
        }
      } else if (currentPage.includes('/pages/order/index')) {
        
        if (!$this.data.status && $this.data.status != '0') {
          currentPage = currentPage
        } else {
          currentPage = currentPage + '?status=' + $this.data.status
        }

      }
    }


    // 测试自定义页面
    let apiurl = api.goods.getAd()
    core.get(apiurl, {

    }, function (result) {
      let adv = result.data

      $this.setData({
        adv: adv
      })
      // 获取间隔时间
      let setTime = adv.intervalTime
      // 获取广告配置信息
      let adverOption = app.getCache('adverOption') || false
      // 显示方式 displaySetup 1(只显示1次) || 2(每次显示) || 3(间隔时间显示)
      if (adv.displayPage == 2 || adv.displayPage == 1) {
        let showKey = false
        
        if (adv.displayPage == 2 && adv.specifiedPage == currentPage) {
          showKey = true
        } else if (adv.displayPage == 1 && '/pages/index/index' == currentPage) {
          showKey = true
        }
        // 指定页面
        if (showKey) {

          if (adv.displaySetup == 1) {
            $this.setData({
              showAdver: adverOption ? adverOption.isShow ? false : true : true
            });
            // 只显示1次
            let adver = {
              isShow: true,
              type: 1
            }
            app.setCache('adverOption', adver)
          } else if (adv.displaySetup == 3) {
            if (adverOption.isShow) {
              return false
            }
            $this.setData({
              showAdver: adverOption ? adverOption.isShow ? false : true : true
            });
            // 间隔显示
            let interTime = Number(setTime) * 60
            let adver
            let adverTime = setInterval(() => {

              if (interTime > 0) {

                interTime--
                adver = {
                  isShow: true,
                  type: 3,
                  time: interTime
                }
              } else {
                adver = {
                  isShow: false,
                  type: 3,
                  time: interTime
                }
                clearInterval(adverTime)
              }
              app.setCache('adverOption', adver)
            }, 1000)
          } else {
            $this.setData({
              showAdver: true
            });
          }
        }
      } else {
        // 全部页面
        if (adv.displaySetup == 1) {
          $this.setData({
            showAdver: adverOption ? adverOption.isShow ? false : true : true
          });
          // 只显示1次
          let adver = {
            isShow: true,
            type: 1
          }
          app.setCache('adverOption', adver)
        } else if (adv.displaySetup == 3) {
          if (adverOption.isShow) {
            return false
          }
          $this.setData({
            showAdver: adverOption ? adverOption.isShow ? false : true : true
          });
          // 间隔显示
          let interTime = Number(setTime) * 60
          let adver
          let adverTime = setInterval(() => {

            if (interTime > 0) {
              interTime--
              adver = {
                isShow: true,
                type: 3,
                time: interTime
              }
            } else {
              adver = {
                isShow: false,
                type: 3,
                time: interTime
              }
              clearInterval(adverTime)
            }

            app.setCache('adverOption', adver)
          }, 1000)
        } else {
          $this.setData({
            showAdver: true
          });
        }
      }

      if (callback) {
        callback(adv);
      }
    });
  }
}